package com.grapro.servlet;

import com.grapro.service.Impl.UserServiceImpl;
import com.grapro.service.UserService;
import util.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Map;

import static java.lang.Integer.parseInt;


@WebServlet("/user.do")
public class UserController extends BaseServlet {

    private UserService userService = new UserServiceImpl();

    public void userLogin(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        int id = parseInt(req.getParameter("id"));
        String password = req.getParameter("password");
        // 判断id和password是否在数据库中存在
        boolean check = userService.loginCheck(id, password);
        if (check) {
            HttpSession session = req.getSession();
            session.setAttribute("id", id);
            Cookie cookie = new Cookie("JSESSIONID", session.getId());
            session.setMaxInactiveInterval(60 * 60 * 24);
            cookie.setMaxAge(60 * 60 * 24);
            resp.addCookie(cookie);

            resp.setHeader("refresh", "0;URL=mainPage.jsp");
        } else {
            resp.getWriter().append("<h1>登录失败</h1>");
            resp.setHeader("refresh", "2;URL=login.html");
        }


    }

    public void showUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession(false);
        int id = (int) session.getAttribute("id");
        Map<String, Object> map = userService.getUserInfo(id);
        req.setAttribute("map", map);
        if (map != null) {
            req.getRequestDispatcher("showUser.jsp").forward(req, resp);
        } else {
            resp.getWriter().append("<h1>查看失败</h1>");
            resp.setHeader("refresh", "1;URL=mainPage.jsp");
        }
    }

    public void updatePassword(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession(false);
        int id = (int) session.getAttribute("id");
        String newPassword1 = (String) req.getParameter("newPassword1");
        String newPassword2 = (String) req.getParameter("newPassword2");
        String oldPassword = (String) req.getParameter("oldPassword");

        if (newPassword1.equals(newPassword2)) {
            boolean ret = userService.updatePassword(id, newPassword1, oldPassword);
            if (ret) {
                resp.getWriter().append("<h1>修改成功</h1>");
                resp.setHeader("refresh", "2;URL=mainPage.jsp");
            } else {
                resp.getWriter().append("<h1>你原密码都输错了--你是不是傻--是不是彪子</h1>");
                resp.setHeader("refresh", "2;URL=updatePassword.jsp");
            }
        } else {
            resp.getWriter().append("<h1>别说你没错--你肯定错了--再好好看看</h1>");
            resp.setHeader("refresh", "2;URL=updatePassword.jsp");
        }

    }

    public void punchCard(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(System.currentTimeMillis());
        HttpSession session = req.getSession(false);
        String username = (String) session.getAttribute("username");
        String name = req.getParameter("name");
        boolean ret = userService.isHaveChecked(format, username);
        boolean flag = false;
        if (name.equals("clockIn")) {
            if (ret) {
                flag = userService.clockIn(format, username);
            } else {
                resp.getWriter().append("<h1>你已经打过卡</h1>");
            }
        } else {
            flag = userService.clockOut(format, username);
        }
        if (flag) {
            resp.getWriter().append("<h1>打卡成功</h1>");
            resp.setHeader("refresh", "2;URL=mainPage.jsp");
        } else {
            resp.getWriter().append("<h1>打卡失败</h1>");
            resp.setHeader("refresh", "2;URL=punchCard.jsp");
        }

    }

    public void addUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Map<String, String[]> parameterMap = req.getParameterMap();
        boolean flag = userService.addEmployee(parameterMap);
        if (flag) {
            resp.getWriter().append("<h1>添加成功，等着老板审核就可以加入大忽悠了</h1>");
            resp.setHeader("refresh", "2;URL=mainPage.jsp");
        } else {
            resp.getWriter().append("<h1>添加失败--我觉得可能是你不太适合我们公司</h1>");
            resp.setHeader("refresh", "2;URL=addUser.jsp");
        }
    }

    public void leaveAndOvertime(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession(false);
        String username = (String) session.getAttribute("username");
        Map<String, String[]> parameterMap = req.getParameterMap();
        boolean flag = userService.insertLO(parameterMap, username);
        if (flag) {
            resp.getWriter().append("<h1>提交成功，等待审核结果</h1>");
            resp.setHeader("refresh", "2;URL=mainPage.jsp");
        } else {
            resp.getWriter().append("<h1>提交失败，请重新提交</h1>");
            resp.setHeader("refresh", "2;URL=leavePage.jsp");
        }
    }
}
